<?php

namespace app\admin\controller;

use think\App;
use think\Request;
use app\admin\services\AuthentService;
use app\admin\services\BaseService;
use think\facade\Session;
use think\facade\Cache;
class Authent extends Base
{
    public function __construct(Request $request, App $app)
    {
        $this->AuthentService = new AuthentService();
        $this->BaseService = new BaseService();
        parent::__construct($request, $app);
        if ($this->checkSession() == false) {
            echo alert_error("用户未登陆，无访问权限!", "/admin/inout/login");
            exit();
        }
        if ($this->checkSessionValue() == false) {
            Session::clear('admin');
            echo alert_error("警告！该账户已在其他地方登陆.", "/admin/inout/login");
            exit();

        }
        //检测cookie是否过期
        if ($this->checkey() == false) {
            $id = session('userid');
            $key = 'login-' . $id;
            Cache::store('redis')->rm($key);
            session(null);
            echo alert_error("该用户已超时，请重新登陆！", "/admin/inout/login");
            exit();

        }

    }


    /**
     * @name 验证session是否存在以及过期
     */
    public function checkSession()
    {
        $result = $this->BaseService->checkSession();
        return $result;
    }


    /**
     * @name 验证session 是否和数据中相等
     */
    public function checkSessionValue()
    {
        $result = $this->BaseService->checkSessionValue();
        return $result;
    }


    /**
     * @name 验证cookie 是否存在以及过期
     */
    public function checkey()
    {
        $result = $this->BaseService->checkey();
        return $result;
    }

}